  <template>
    <div class="frame-header" :style="opacityStyle">
      <div class="frame-header-content">
        <div class="frame-logo">
          <!--<img src="https://www.newtouch.com/static/img/1_logo.png" alt="">-->
          <span>开源企业</span>
        </div>
        <div class="frame-menu-wrapper">
          <el-button class="module" type="text" v-if="getLocalToken()" @click="appClick('organization')">组织架构</el-button>
          <el-button class="module" type="text" @click="appClick('devTool')">开发者平台</el-button>
          <div class="frame-menu">
            <div class="profile" v-if="getLocalToken()">
              <el-dropdown @command="handleCommand">
                <span class="el-dropdown-link">
                  {{currentUser.name}}<i class="el-icon-arrow-down el-icon--right"></i>
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="personalInfo">个人信息</el-dropdown-item>
                  <el-dropdown-item command="logout">退出</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
              <el-image style="width: 30px; height: 30px" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.win4000.com%2Fwallpaper%2Ff%2F54508f963d9b8.jpg&refer=http%3A%2F%2Fpic1.win4000.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1673708193&t=211f7faaba0158f492cb46306b54509c" fit="fit"></el-image>
            </div>
            <div class="profile" v-else>
              <el-button type="primary" @click="toLogin">登录</el-button>
            </div>
          </div>
        </div>
      </div>

      <personal-info ref="personalInfo" v-on:username-changed="doGetCurrentUsername"></personal-info>
    </div>
  </template>

<script>
import { logOut } from '@/api/auth'
import { getLocalToken, getLocalCurrentUser, clearLocalData } from '@/utils/auth'
import PersonalInfo from '@/views/admin/common/personalInfo'
export default {
  name: 'Header',
  components: { PersonalInfo },
  data() {
    return {
	  getLocalToken,
	  showHeader: true,
	  opacityStyle: {},
	  currentUser: getLocalCurrentUser(),
    }
  },
  mounted() {
	window.addEventListener('scroll', this.handleScroll)
  },
  methods: {
	toLogin() {
      this.$emit('toLoginEmit')
    },
  appClick(appKey) {
    this.$emit('app-click', appKey)
  },
	handleCommand(command) {
	  switch(command) {
		case 'logout':
		  this.doLogOut()
		  break
		case 'personalInfo':
		  this.$refs.personalInfo.$emit('openPersonalInfoDialog')
		  break
	  }
    },
	handleScroll(){
	  const top = document.documentElement.scrollTop;
	  if(top > 5) {
        this.opacityStyle = {
          position: 'fixed',
		  'box-shadow': '0 -1px 10px #e6e6e6'
        }
	  } else {
		this.opacityStyle = {}
	  }
	},
	doGetCurrentUsername() {
	  this.currentUser = getLocalCurrentUser()
	},
	doLogOut() {
	  this.$confirm('确定要退出吗?', '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning'
	  }).then(() => {
		logOut().then(responseData => {
		  clearLocalData()
		  this.$router.go(0)
		})
		.catch(error => {})
	  }).catch(() => {});
	}
  },
  destroyed(){
	window.removeEventListener('scroll', this.handleScroll)
  }
}
</script>

<style scoped lang="scss">
  @import '../../../styles/element-variables.scss';
  .frame-header {
    top: 0;
    left: 0;
    width: 100%;
    background-image: none;
    background-color: #fff;
    transition: all .3s;
    z-index: 100;
    .frame-header-content {
      padding: 0 16px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      width: 1280px;
      margin: 0 auto;
      .el-menu {
        .el-menu-item {
          font-size: 14px;
        }
      }
    }
    .frame-logo {
      img {
        width: 120px;
      }
      span {
        font-size: 18px;
        font-weight: bold;
        color: $--color-primary;
      }
    }
    .frame-menu-wrapper {
      display: flex;
      .module {
        margin: 0 20px;
        color: #333;
      }
    }
    .frame-menu {
      &.el-menu.el-menu--horizontal {
        border-bottom: none;
      }
      .el-menu-item {
        height: 60px;
        line-height: 60px;
        border-bottom: none;
        i {
          margin-right: 0;
        }
        &.is-active {
          border-bottom: none;
          border-bottom: 0;
          position: relative;
        }
        &:after {
          display: none;
          position: absolute;
          content: '';
          height: 3px;
          width: 0;
          background-color: $--color-primary;
          bottom: 0;
          left: 50%;
          transition: all .3s;
        }
        &.is-active:after {
          width: 20px;
          margin-left: -10px;
        }
      }
      .profile {
        position: relative;
        padding-left: 20px;
        height: 60px;
        color: #333;
        font-weight: bold;
        display: flex;
        align-items: center;
        &:before {
          position: absolute;
          content: '';
          width: 1px;
          height: 16px;
          background-color: #dbdbdb;
          top: 50%;
          margin-top: -8px;
          left: 0;
        }
        .el-image {
          margin-left: 20px;
          border-radius: 50%;
        }
      }
    }
  }
</style>
